package com.tender;

import com.tender.utils.ESOperationTemplate;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;

import java.util.Map;

public class A_17_QueryHighLightDoc {

    public static void main(String[] args) throws Exception {
        ESOperationTemplate.process(client -> {

            try {
                // 高亮查询
                SearchRequest request = new SearchRequest().indices("user2");
                //2.创建查询请求体构建器
                SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
                //构建查询方式：高亮查询
                TermsQueryBuilder termsQueryBuilder =
                        QueryBuilders.termsQuery("name","zhangsan");
                //设置查询方式
                sourceBuilder.query(termsQueryBuilder);
                //构建高亮字段
                HighlightBuilder highlightBuilder = new HighlightBuilder();
                highlightBuilder.preTags("<font color='red'>");//设置标签前缀
                highlightBuilder.postTags("</font>");//设置标签后缀
                highlightBuilder.field("name");//设置高亮字段
                //设置高亮构建对象
                sourceBuilder.highlighter(highlightBuilder);
                //设置请求体
                request.source(sourceBuilder);
                //3.客户端发送请求，获取响应对象
                SearchResponse response = client.search(request, RequestOptions.DEFAULT);
                //4.打印响应结果
                SearchHits hits = response.getHits();
                System.out.println("took::"+response.getTook());
                System.out.println("time_out::"+response.isTimedOut());
                System.out.println("total::"+hits.getTotalHits());
                System.out.println("max_score::"+hits.getMaxScore());
                System.out.println("hits::::>>");
                for (SearchHit hit : hits) {
                    String sourceAsString = hit.getSourceAsString();
                    System.out.println(sourceAsString);
                    //打印高亮结果
                    Map<String, HighlightField> highlightFields = hit.getHighlightFields();
                    System.out.println(highlightFields);
                }
                System.out.println("<<::::");

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return null;
        });
    }

}
